package com.sesame.mq.rabbit.direct.controller;

import com.alibaba.fastjson.JSON;
import com.sesame.bean.User;
import com.sesame.mq.rabbit.direct.config.RabbitDirectConfig;
import kim.sesame.common.utils.UUIDUtil;
import kim.sesame.common.web.controller.AbstractWebController;
import kim.sesame.common.result.ApiResult;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CommonsLog
@RestController
@RequestMapping("/direct")
public class DirectSendController extends AbstractWebController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * http://127.0.0.1:2020/rabbitmq/direct/send?name=aaa
     */
    @RequestMapping("/send")
    public ApiResult directSend(String name) {
        User user = new User(UUIDUtil.getShortUUID(), name);

        String json = JSON.toJSONString(user);
        amqpTemplate.convertAndSend(RabbitDirectConfig.exchange, RabbitDirectConfig.directRouteKey, json);

        return success();
    }

}
